package com.zxstudio.bulls.stock.user.controller;

import com.zxstudio.bulls.stock.common.exception.BusinessException;
import com.zxstudio.bulls.stock.common.exception.ComponentException;
import com.zxstudio.bulls.stock.common.web.vo.ApiRespResult;
import com.zxstudio.bulls.stock.common.web.vo.user.TradeUserVo;
import com.zxstudio.bulls.stock.user.service.IStockUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/open")
@Log4j2
public class StockUserOpenController {

    @Autowired
    private IStockUserService stockUserService;

    /**
     * 用户注册接口
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ApiRespResult register(@Valid TradeUserVo tradeUser) {

        ApiRespResult result = null;
        try {
            stockUserService.userRegister(tradeUser);
            result = ApiRespResult.success(tradeUser);
        }catch(ComponentException e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.error(e.geterrorCodeEnum());
        }catch(BusinessException e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.error(e.geterrorCodeEnum());
        }catch(Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }

        return result;
    }

}
